# -*- coding: utf-8 -*-
# @Time : 2023/10/29 2:16
# @Author : zxp

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 8001))
sock.listen(5)

conn, addr = sock.accept()

# 接收文件大小
data = conn.recv(1024)
data_size = int(data.decode('utf-8'))

# 接收文件
file_object = open('xxx.png', 'wb')
reply_size = 0

while True:
    data = conn.recv(1024)
    file_object.write(data)
    file_object.flush()

    reply_size += len(data)
    if reply_size == data_size:
        break
sock.close()
